package uk.ac.ebi.das.uniprot.image.model;

import org.biodas.jdas.schema.features.FEATURE;
import uk.ac.ebi.das.uniprot.image.util.FeatureTypeConfig;
import uk.ac.ebi.das.uniprot.image.util.FeatureTypeConfigHelper;

/**
 * Created by IntelliJ IDEA.
 * User: ljgarcia
 * Date: 08/02/12
 * Time: 16:51
 * To change this template use File | Settings | File Templates.
 */
public class PositionalFeature extends FEATURE implements Comparable<PositionalFeature> {
    /**
     * Cartoon cartoonType
     */
    FeatureTypeConfig cartoonType;

    /**
     * Default constructor.
     */
    public PositionalFeature() {
        super();
        this.cartoonType = FeatureTypeConfig.UNKNOWN;
    }

    /**
     * Copy constructor, takes a feature and convert it into a Postional Feature.
     * @param feature
     */
    public PositionalFeature(FEATURE feature) {
        this.setEND(feature.getEND());
        this.setId(feature.getId());
        this.setLabel(feature.getLabel());
        this.setMETHOD(feature.getMETHOD());
        this.setORIENTATION(feature.getORIENTATION());
        this.setPHASE(feature.getPHASE());
        this.setSCORE(feature.getSCORE());
        this.setSTART(feature.getSTART());
        this.setTYPE(feature.getTYPE());
        if (feature.getGROUP() != null) { this.getGROUP().addAll(feature.getGROUP()); }
        if (feature.getLINK() != null) { this.getLINK().addAll(feature.getLINK()); }
        if (feature.getNOTE() != null) { this.getNOTE().addAll(feature.getNOTE()); }
        if (feature.getPARENT() != null) { this.getPARENT().addAll(feature.getPARENT()); }
        if (feature.getPART() != null) { this.getPART().addAll(feature.getPART()); }
        if (feature.getTARGET() != null) { this.getTARGET().addAll(feature.getTARGET()); }
        if (feature.getTYPE() == null) {
            this.cartoonType = FeatureTypeConfig.UNKNOWN;
        } else {
            if (feature.getTYPE().getCvId() == null) {
                this.cartoonType = FeatureTypeConfig.UNKNOWN;
            } else {
                this.cartoonType = FeatureTypeConfigHelper.fetchByCvId(feature.getTYPE().getCvId(), feature.getId());
            }
        }
    }

    /**
     * Gets the cartoonType.
     * @return
     */
    public FeatureTypeConfig getCartoonType() {
        return cartoonType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        PositionalFeature that = (PositionalFeature) o;

        //System.out.println(this.toString() + " - " + that.toString());
        if (this.getEND() != null) {
            if (that.getEND() != null) {
                if (this.getEND().intValue() != that.getEND().intValue()) {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            if (that.getEND() != null) {
                return false;
            }
        }

        if (this.getSTART() != null) {
            if (that.getSTART() != null) {
                if (this.getSTART().intValue() != that.getSTART().intValue()) {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            if (that.getSTART() != null) {
                return false;
            }
        }

        if (this.getTYPE() != null) {
            if (that.getTYPE() != null) {
                if (this.getTYPE().getCvId() != null) {
                    if (that.getTYPE().getCvId() != null) {
                        if (!this.getTYPE().getCvId().equals(that.getTYPE().getCvId())) {
                            return false;
                        }
                    } else {
                        return false;
                    }
                } else {
                    if (that.getTYPE().getCvId() == null) {
                        return false;
                    }
                }
            } else {
                return false;
            }
        } else {
            if (that.getTYPE() != null) {
                return false;
            }
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = this.getSTART() != null ? this.getSTART().intValue() : 0;
        result = 31 * result + (this.getEND() != null ? this.getEND().intValue() : 0);
        result = 31 * result + (this.getTYPE() != null ? (this.getTYPE().getCvId() != null ? this.getTYPE().getCvId().hashCode() : 0) : 0);
        return result;
    }

    @Override
    public int compareTo(PositionalFeature f) {
        int start = this.getSTART() == null ? 0 : this.getSTART().intValue();
        int end = this.getEND() == null ? 0 : this.getEND().intValue();
        int cvid = this.getTYPE() == null ? 0 : (this.getTYPE().getCvId() == null ? 0 : this.getTYPE().getCvId().length());

        int fStart = f.getSTART() == null ? 0 : f.getSTART().intValue();
        int fEnd = f.getEND() == null ? 0 : f.getEND().intValue();
        int fCvid = f.getTYPE() == null ? 0 : (f.getTYPE().getCvId() == null ? 0 : f.getTYPE().getCvId().length());

        if (start == fStart) {
            if (end == fEnd) {
                if (this.getTYPE() != null) {
                    if (this.getTYPE().getCvId() != null) {
                        if (f.getTYPE() != null) {
                            if (f.getTYPE().getCvId() != null) {
                                if (this.getTYPE().getCvId().equals(f.getTYPE().getCvId())) {
                                    return 0;
                                } else {
                                    return this.getTYPE().getCvId().length() - f.getTYPE().getCvId().length();
                                }
                            } else {
                                return 1;
                            }
                        } else {
                            return 1;
                        }
                    } else {
                        return -1;
                    }
                } else {
                    return -1;
                }
            } else {
                return end - fEnd;
            }
        } else {
            return start - fStart;
        }
    }

    @Override
    public String toString() {
        return "<" + this.getId() + "," + this.getTYPE().getCvId() + "," + this.getSTART().intValue() + "," + this.getEND().intValue() + ">";
    }
}
